package slap.util;

import static jodd.JoddDefault.ioBufferSize;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

import jodd.io.FastByteArrayOutputStream;
import jodd.io.StreamUtil;

public class GZIP {
	
	public static byte[] compress(InputStream raw) throws IOException{
		FastByteArrayOutputStream bo = new FastByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(bo);
		StreamUtil.copy(raw, gzip);
		gzip.flush();
		gzip.close();
		return bo.toByteArray();
	}
	
	public static void compress(InputStream input, OutputStream output) throws IOException{
		GZIPOutputStream gzip = new GZIPOutputStream(output);
		StreamUtil.copy(input, gzip);
		gzip.flush();
		gzip.close();
	}
	
	public static byte[] compress(String raw) throws IOException{
		return compress(raw.getBytes());
	}

	public static byte[] compress(byte[] raw) throws IOException {
		FastByteArrayOutputStream bo = new FastByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(bo);
		gzip.write(raw);
		gzip.flush();
		gzip.close();
		return bo.toByteArray();
	}
}
